//
//  ZLWaveView.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/11.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "ZLWaveView.h"

@interface ZLWaveView() {
    CGFloat offsetX; // 位移
    CGFloat waveSpeed; // 水纹速度
    CGFloat currentK; // 当前波浪高度
    CGFloat waterWaveWidth; //水纹路宽度
    CGFloat waveA; // 水纹的振幅
    CGFloat waveW; // 周期
    CADisplayLink *waveDisplaylink;
    CAShapeLayer *firstWaveLayer;
    CAShapeLayer *secondWaveLayer;
}

@end

@implementation ZLWaveView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];
    
    waveDisplaylink = [[CADisplayLink alloc] init];
    firstWaveLayer = [[CAShapeLayer alloc] init];
    secondWaveLayer = [[CAShapeLayer alloc] init];
    
    
    [self.layer setMasksToBounds:true];
    
    // 设置闭环颜色
    firstWaveLayer.fillColor = [UIColor colorWithRed:73/255.0 green:142/255.0 blue:178/255.0 alpha:0.5].CGColor;
//    firstWaveLayer.fillColor = UIColor.clearColor.CGColor;
    // 设置边缘线颜色
//    firstWaveLayer.strokeColor = UIColor.blueColor.CGColor;
    firstWaveLayer.strokeStart = 0.0;
    firstWaveLayer.strokeEnd = 1.0;
    
    // 设置第二条wave
    secondWaveLayer.fillColor = firstWaveLayer.fillColor;
    secondWaveLayer.strokeStart = 0.0;
    secondWaveLayer.strokeEnd = 1.0;
    
    // 添加layer
    [self.layer addSublayer:firstWaveLayer];
    [self.layer addSublayer:secondWaveLayer];
    
    
    // 波浪宽度
    waterWaveWidth = self.bounds.size.width;
    // 设置波浪流动速度
    waveSpeed = 0.05;
    // 设置振幅
    waveA = 8;
    // 设置周期
    waveW = 2*(CGFloat)M_PI_2 / waterWaveWidth;
    // 设置波浪纵向位置
    currentK = self.bounds.size.height / 2;
    
    
    waveDisplaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];
    [waveDisplaylink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

- (void) getCurrentWave:(CADisplayLink *)displayLink {
    // 实时位移
    offsetX += waveSpeed;
    [self setWaveLayerPaths];
}

- (void) setWaveLayerPaths {
//    firstWaveLayer.path = path;
    [self setWaveLayerPathAtIndex:0];
    [self setWaveLayerPathAtIndex:1];
    
}

/** cindex表示第几条水纹 */
- (void)setWaveLayerPathAtIndex:(NSInteger)cindex {
    CAShapeLayer *cLayer = (CAShapeLayer *)self.layer.sublayers[cindex];
    if (![cLayer isKindOfClass:[CAShapeLayer class]]) {
        return;
    }

    // 创建一个路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat y = currentK;
    CGPathMoveToPoint(path, NULL, 0, y);

    for (int i=0; i<waterWaveWidth; i++) {
        y = waveA * sin(waveW * (CGFloat)i + offsetX - waterWaveWidth*cindex/2) + currentK;
        CGPathAddLineToPoint(path, NULL, (CGFloat)i, y);
    }
    
    CGPathAddLineToPoint(path, NULL, waterWaveWidth, self.bounds.size.height);
    CGPathAddLineToPoint(path, NULL, 0, self.bounds.size.height);
    CGPathCloseSubpath(path);
    
    cLayer.path = path;
}

- (void)dealloc {
    [waveDisplaylink invalidate];
}

@end
